package com.qianguo.shop.fragment.tab;

import android.app.Activity;
import android.widget.TextView;

import com.eva.androidlibrary.api.entity.ImageBean;
import com.eva.androidlibrary.api.entity.ResultData;
import com.eva.androidlibrary.image.ImageLoader;
import com.eva.androidlibrary.widget.CircleImageView;
import com.qianguo.shop.R;
import com.qianguo.shop.UserManager;
import com.qianguo.shop.activity.ModifyMobileActivity;
import com.qianguo.shop.activity.ModifyNickNameActivity;
import com.qianguo.shop.activity.ModifyPasswordActivity;
import com.qianguo.shop.activity.RealNameAuthActivity;
import com.qianguo.shop.activity.ShopMainActivity_;
import com.eva.androidlibrary.api.HttpRequestCallback;
import com.qianguo.shop.base.BaseChooseImageActivity;
import com.qianguo.shop.base.BaseFragment;
import com.eva.androidlibrary.api.entity.UserInfo;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.io.File;


/**
 * 我的tab
 *
 * @author GUXIA318
 * @date 2015-09-14 17:30
 */
@EFragment(R.layout.fragment_tab_business_my_center)
public class MyCenterTabFragment extends BaseFragment {

    @ViewById(R.id.user_center_avatar_image_view)
    CircleImageView mImgAvatar;

    @ViewById(R.id.user_center_name_text_view)
    TextView mTvRealname;

    @ViewById(R.id.user_center_nick_name_text_view)
    TextView mTvNickname;

    @ViewById(R.id.user_center_authentication_text_view)
    TextView mTvAuthStatus;

    private ShopMainActivity_ mActivity;

    private UserInfo mUser;


    /**
     * 修改头像
     */
    @Click(R.id.user_center_avatar_layout)
    void clickAvatar() {
        mActivity.showPickerDialog(mPicPickerCallback);
    }

    private BaseChooseImageActivity.PicPickerCallback mPicPickerCallback = new BaseChooseImageActivity.PicPickerCallback() {
        @Override
        public void afterPick(String filePath) {
            mActivity.crop(filePath, mPicCropCallback);
        }
    };

    private BaseChooseImageActivity.PicCropCallback mPicCropCallback = new BaseChooseImageActivity.PicCropCallback() {
        @Override
        public void afterCrop(File file) {
            uploadAvatar(mUser.getToken(), file);
            updateAvatar(file.getAbsolutePath());
        }
    };

    /**
     * 上传头像
     *
     * @param token
     * @param file
     */
    @Background
    void uploadAvatar(final String token, File file) {
        mApp.getApi().uploadImg(token, file, new HttpRequestCallback<ImageBean>() {
            @Override
            public void onSuccess(int code, String msg, ImageBean data) {
                editAvatar(token, data);
            }

            @Override
            public void onFailure(int code, String msg) {
                showSnackBar(getActivity(), msg);
            }
        });
    }

    /**
     * 修改头像
     *
     * @param token
     * @param imageBean
     */
    void editAvatar(String token, ImageBean imageBean) {
        mApp.getApi().editAvatar(token, imageBean.getUrl(), new HttpRequestCallback<ResultData>() {
            @Override
            public void onSuccess(int code, String msg, ResultData data) {

            }

            @Override
            public void onFailure(int code, String msg) {
                showSnackBar(getActivity(), msg);
            }
        });
    }

    /**
     * 修改昵称
     */
    @Click(R.id.user_center_nick_name_layout)
    void clickNickName() {
        ModifyNickNameActivity.jumpToModifyNickname(getActivity(), mUser);
    }

    /**
     * 实名认证
     */
    @Click(R.id.user_center_authentication_layout)
    void clickAuthentication() {
        RealNameAuthActivity.jumpToRealName(getActivity());
    }

    /**
     * 改绑手机
     */
    @Click(R.id.user_center_change_mobile_layout)
    void clickChangeMobile() {
        ModifyMobileActivity.jumpToModifyMobile(getActivity(), mUser);
    }

    /**
     * 修改密码
     */
    @Click(R.id.user_center_modify_password_layout)
    void clickModifyPassword() {
        ModifyPasswordActivity.jumpToModifyPassword(getActivity());
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof ShopMainActivity_) {
            mActivity = (ShopMainActivity_) activity;
        }
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void initializeUI() {
        refreshProfile();
        getUserInfo();
    }

    @UiThread(propagation = UiThread.Propagation.REUSE)
    void refreshProfile() {
        mUser = mApp.getUser();
        mImageLoader.loadImage(getActivity(), mImgAvatar, mUser.getAvatar());
        mTvRealname.setText(mUser.getRealname());
        mTvNickname.setText(mUser.getNickname());

        // 实名认证状态
        int authStatus = mUser.getIsRealName();
        switch (authStatus) {
            case UserInfo.AUTH_STATUS_YES:
                mTvAuthStatus.setText(getString(R.string.user_center_auth_status_yes));
                break;
            case UserInfo.AUTH_STATUS_NO:
                mTvAuthStatus.setText(getString(R.string.user_center_auth_status_no));
                break;
            case UserInfo.AUTH_STATUS_ING:
                mTvAuthStatus.setText(getString(R.string.user_center_auth_status_ing));
                break;
        }

    }

    @Background
    void getUserInfo() {
        mApp.getApi().getProfile(mUser.getToken(), new HttpRequestCallback<UserInfo>() {
            @Override
            public void onSuccess(int code, String msg, UserInfo data) {
                UserManager.updateUser(mApp, data);
                refreshProfile();
            }

            @Override
            public void onFailure(int code, String msg) {
                showSnackBar(getActivity(), msg);
            }
        });
    }

    @UiThread
    public void updateAvatar(String filePath) {
        ImageLoader.getInstance(mActivity).loadImage(mActivity, mImgAvatar, filePath, R.mipmap.default_avatar);
    }

    @Subscribe
    public void onEvent(UserInfo user) {
        refreshProfile();
    }

}